# Contributor: mio <miyopan@e.email>
# Maintainer: mio <miyopan@e.email>
pkgname=tic-80
pkgver=1.1.2837
# gitrev is used to insert the correct version hash during build
_gitrev="be42d6f146cfa520b9b1050feba10cc8c14fb3bd"
pkgrel=2
pkgdesc="Fantasy computer for making, playing and sharing tiny games"
url="https://tic80.com/"
license="MIT"
# ppc64le: build failed with libpng.a error
# riscv64: blocked by janet-dev
arch="all !ppc64le !riscv64"
makedepends="
	cmake
	curl-dev
	doxygen
	freeglut-dev
	giflib-dev
	glu-dev
	http-parser-dev
	jack-dev
	janet-dev
	libpng-dev
	libuv-dev
	lua5.3-dev
	pulseaudio-dev
	ruby-dev
	samurai
	sdl2-dev
	zlib-dev
	"
builddir="$srcdir"/TIC-80-"$pkgver"
options="!check" # No tests

# Submodules list from https://github.com/nesbox/TIC-80/tree/v$pkgver/vendor
# moonscript: vendor library contains a required header file
# mruby: repo mruby-dev errors when building vendor zip (zip.co.o)
# quickjs: repo quickjs-dev errors when linking lib/tic80_libretro.so
# sokol: not enabled by default, not included as dependency
# zip: a minizip fork not directly replaceable by repo zip
# s7: no gitrev in vendor/, using latest gitrev from upstream
_argparse_rev="0d5f5d0745df14a3f373f7eed85bf524714f4524"
_blip_buf_rev="330226d9b55ecbeea644e17b5e0f096a165ca07e"
_dirent_rev="c885633e126a3a949ec0497273ec13e2c03e862c"
_lpeg_rev="73d8614a8dea404cf7bfe25a6e4cea7183dc9fb7"
_moonscript_rev="17179283012b392bff972ad66231d73bfeec6e90"
_mruby_rev="7fe6f3976eca19a86fe533deb4c60b31cd80a236"
_msf_gif_rev="7829c8f439d013deeb54eba94761403e1da2a960"
_naett_rev="10a9624456829e5f2b4c264e14760301125d12eb"
_pocketpy_rev="7312afdad24bb308037088d34b32ed0078fed7d2"
_quickjs_rev="b7c0161f2ad1360f0837abafc6abf0edb82c0fa7"
_s7_rev="60c3805e24d3ace68d8710e26d175c5c5dadcc74"
_squirrel_rev="9dcf74f99097898dd5a111c4a55b89d1c4d606c0"
_wasm3_rev="a3abb3f2d320994be59dfbe2cdafa8d0b6a8253e"
_wren_rev="4a18fc489f9ea3d253b20dd40f4cdad0d6bb40eb"
_zip_rev="d7df626f3aa457e01669f65e61bf8f484e352941"
_submodules="
	argparse-$_argparse_rev
	blip-buf-$_blip_buf_rev
	dirent-$_dirent_rev
	lpeg-$_lpeg_rev
	moonscript-$_moonscript_rev
	mruby-$_mruby_rev
	msf_gif-$_msf_gif_rev
	naett-$_naett_rev
	pocketpy-$_pocketpy_rev
	quickjs-$_quickjs_rev
	s7-$_s7_rev
	squirrel-$_squirrel_rev
	wasm3-$_wasm3_rev
	wren-$_wren_rev
	zip-$_zip_rev
	"
source="tic-80-$pkgver.tar.gz::https://github.com/nesbox/TIC-80/archive/v$pkgver/tic80-v$pkgver.tar.gz
	argparse-$_argparse_rev.tar.gz::https://github.com/cofyc/argparse/archive/$_argparse_rev.tar.gz
	blip-buf-$_blip_buf_rev.tar.gz::https://github.com/nesbox/blip-buf/archive/$_blip_buf_rev.tar.gz
	dirent-$_dirent_rev.tar.gz::https://github.com/tronkko/dirent/archive/$_dirent_rev.tar.gz
	lpeg-$_lpeg_rev.tar.gz::https://github.com/nesbox/lpeg/archive/$_lpeg_rev.tar.gz
	moonscript-$_moonscript_rev.tar.gz::https://github.com/nesbox/moonscript/archive/$_moonscript_rev.tar.gz
	mruby-$_mruby_rev.tar.gz::https://github.com/mruby/mruby/archive/$_mruby_rev.tar.gz
	msf_gif-$_msf_gif_rev.tar.gz::https://github.com/notnullnotvoid/msf_gif/archive/$_msf_gif_rev.tar.gz
	naett-$_naett_rev.tar.gz::https://github.com/erkkah/naett/archive/$_naett_rev.tar.gz
	pocketpy-$_pocketpy_rev.tar.gz::https://github.com/blueloveTH/pocketpy/archive/$_pocketpy_rev.tar.gz
	quickjs-$_quickjs_rev.tar.gz::https://github.com/nesbox/quickjs/archive/$_quickjs_rev.tar.gz
	s7-$_s7_rev.tar.gz::https://cm-gitlab.stanford.edu/bil/s7/-/archive/$_s7_rev.tar.gz
	squirrel-$_squirrel_rev.tar.gz::https://github.com/albertodemichelis/squirrel/archive/$_squirrel_rev.tar.gz
	wasm3-$_wasm3_rev.tar.gz::https://github.com/wasm3/wasm3/archive/$_wasm3_rev.tar.gz
	wren-$_wren_rev.tar.gz::https://github.com/wren-lang/wren/archive/$_wren_rev.tar.gz
	zip-$_zip_rev.tar.gz::https://github.com/kuba--/zip/archive/$_zip_rev.tar.gz
	use-repo-deps.patch
	"

prepare() {
	default_prepare
	cd "$srcdir"

	# Copy extracted submodules into the main package source
	_vendor_dir="$srcdir"/TIC-80-"$pkgver"/vendor/
	# s7 submodule: replace with upstream version
	rm -r "$_vendor_dir"/s7
	for _mod in $_submodules; do
		_mod_rev=${_mod##*-}
		_mod_name=$(echo $_mod | sed -e "s/-$_mod_rev//")
		mv $_mod $_mod_name
		mv $_mod_name $_vendor_dir
	done

	# Set version revision for executable to display the correct version
	_version_rev=${pkgver##*\.}
	sed -i "s/VERSION_REVISION 0/VERSION_REVISION $_version_rev/" \
		TIC-80-"$pkgver"/CMakeLists.txt
}

build() {
	cmake -B build-tic -G Ninja \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DVERSION_HASH=${_gitrev:0:7} \
		-DCMAKE_BUILD_WITH_INSTALL_RPATH=ON
	cmake --build build-tic
}

package() {
	DESTDIR="$pkgdir" cmake --install build-tic

	# Extra tools need manual install
	for tool in build-tic/bin/*; do
		install -Dm755 $tool -t "$pkgdir"/usr/bin
	done
}

sha512sums="
9e42924923bf2f1e7b1f6edd54a0f1f8c930f634115f54ff3ae1f2fbeb15b46837598713eb689691d9bff9abed22ef0577e0360f7a2dd453b17fd1f722e35abb  tic-80-1.1.2837.tar.gz
2dba667249c19ec470421327f6cc20f11a168847a99f60db395980c28339a269041a778645355d8ecbac76e1eac0b77c12f3f2fb3573e0e372ccfffdc947ca1c  argparse-0d5f5d0745df14a3f373f7eed85bf524714f4524.tar.gz
69c42cd93cbb640952244c44cdaf75e0808536ca37300912d026c2aedc878b5f105ae109a5eef1a558b1fdc8a692112491515c6813113a007878908224eba5d8  blip-buf-330226d9b55ecbeea644e17b5e0f096a165ca07e.tar.gz
ce4a2b445073b3bb82d31934ce2d1dcea771723d4cf1778e065cfe787e91128d995cfccc96d860aceb3637f35627dfbf597e423967f08c5ecf855992e9a84581  dirent-c885633e126a3a949ec0497273ec13e2c03e862c.tar.gz
93956abcafe84d200fab41d54686cbba6ac8eae91292dc4282186543edef8211e2768dab83854b149e21cb0775b1c2ef8fd79b94f3f72a860b9710841c843b2e  lpeg-73d8614a8dea404cf7bfe25a6e4cea7183dc9fb7.tar.gz
05a26627c1a8686d876654751e590c024aa603fff6ff75ed4ed61d499c16c9fef5616222a6dfc5f11aaf9a7ed79786f92e5a15ca8a42fa79c5adba3228f3bdad  moonscript-17179283012b392bff972ad66231d73bfeec6e90.tar.gz
0211031f5ed457d0872f34dc959468c2c6f545c5eb76c878b6fb867df333e5c534e9d311fb8bf30d3c8b2624bcae1e54f92719060faa47211a9883151a19d977  mruby-7fe6f3976eca19a86fe533deb4c60b31cd80a236.tar.gz
0f02b6ef21372ea744ce46d14072887acfa6b8e67d5b39a8b63b87a15e87f414cb3f4635e0f99f1a037904b65fb68b0186d2904761631ae920c7874b7f0e1bf1  msf_gif-7829c8f439d013deeb54eba94761403e1da2a960.tar.gz
e17246e5b04120388cfb688bc9bf1895fcd198351b856a4f3da9116ea0091334ffaaca1cdf3a7f6df99c0181037e564c0b2501126617d93d5f37b84f8a6420f7  naett-10a9624456829e5f2b4c264e14760301125d12eb.tar.gz
392ef1fe0d352c20ec7ede4676d0f1d6236c0d7d5e9db00594a84132b35bdb38d71a7fd09ee42bc5ee6607d882fdf2e31faeaf545f6a986c42b6f6771bd834f9  pocketpy-7312afdad24bb308037088d34b32ed0078fed7d2.tar.gz
e7d5f7a9a39f4a7f93e8851f5f916e4122bd95c50efe52f864dbc5ec0aee31b76638cdc7d24059e5b6051d0e4021764d97ef37b596115c0991ebfac4274ba95e  quickjs-b7c0161f2ad1360f0837abafc6abf0edb82c0fa7.tar.gz
161c3768c3ce2106e32a1ca86f1e8ef2cc424fb54f03e7fe22856822a1afcebbe69f219677e66ebbb798edcc48e40cbc87d5966d6d5e26c9b17e348542d0ad14  s7-60c3805e24d3ace68d8710e26d175c5c5dadcc74.tar.gz
0bc2e0842b429854a8f61f2ed0b2261f6af49849f1a90f2fc2aa3578a892b2a97131dcc209eb9c7ceda5544162542a757d4ca091797f0fc0324664f3cfe7fc7e  squirrel-9dcf74f99097898dd5a111c4a55b89d1c4d606c0.tar.gz
e23e24b697c068b27a5aba71dec33d84b5079155344aa52d2997e2267986167df30ee3e264ad8ff40c62ae73b95eb48896b9e8a47bf9904cc23bbae61818e245  wasm3-a3abb3f2d320994be59dfbe2cdafa8d0b6a8253e.tar.gz
b3d79e9cb647e52db06d5cddfc8c93d05ae0e8d87f0f879ac2b812fcc7f55e018d21d3b04d62eaeb12e6d931b5e43fbe357b187e7f446e86e39be015c51c2eee  wren-4a18fc489f9ea3d253b20dd40f4cdad0d6bb40eb.tar.gz
ebfa73418963010277f112e1e652c8da54994087a1d8095c527f75a799e4d0933e4ff78bdb361cedb1c844b167145fe010c2805a08c2825476f755899eb68fdc  zip-d7df626f3aa457e01669f65e61bf8f484e352941.tar.gz
230712b0d3c42d924bc5f2b05e9c3aeac7ddd1fba4971ce9a43d4f67585cc1c89fba666c3a0c3fb43e227f7410b8cd6cbb9211bc4fcf39b7079c3f8f44ea210c  use-repo-deps.patch
"
